package eu.dominicum.ft.pd;

import eu.dominicum.ft.pd.mao.MakeStageResultDetailMAO;
import eu.dominicum.ft.pd.mao.MakeStageResultMAO;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.ValidationException;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Stage result.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
public class StageResult extends PersistentDomainObject {

    /**
     * The associated Stage.
     */
    private Stage stage;

    /**
     * The result type.
     */
    private EStageResultType resultType;

    /**
     * The location.
     */
    private Location location;

    /**
     * Associated stage result details.
     */
    private final List<StageResultDetail> stageResultDetails
            = new ArrayList<>();

    /**
     * Make a StageResult object.
     *
     * @param makeStageResultMAO -
     * @return StageResult
     */
    public static StageResult make(
            final MakeStageResultMAO makeStageResultMAO) {

        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        Validator validator = validatorFactory.getValidator();
        Set<ConstraintViolation<MakeStageResultMAO>> violations = validator.validate(makeStageResultMAO);

        if (!violations.isEmpty()) {
            throw new ValidationException((violations.iterator().next().getMessage()));
        }


        StageResult newStageResult = new StageResult();

        newStageResult.resultType = makeStageResultMAO.getResultType();
        newStageResult.stage = makeStageResultMAO.getStage();
        if (makeStageResultMAO.getLocation() != null) {
            newStageResult.location = makeStageResultMAO.getLocation();
        } else {
            newStageResult.location =
                    Location.make(makeStageResultMAO.getMakeLocationMAO());
        }

        for (MakeStageResultDetailMAO makeStageResultDetailMAO
                : makeStageResultMAO.getMakeStageResultDetailsMAOs()) {
            makeStageResultDetailMAO.setStageResult(newStageResult);
            StageResultDetail newStageResultDetail = StageResultDetail.make(makeStageResultDetailMAO);
            newStageResult.stageResultDetails.add(newStageResultDetail);
        }
        return newStageResult;
    }

    /**
     * Protected constructor to satisfy JPA based object stores.
     */
    protected StageResult() {

    }

    /**
     * .
     *
     * @return Stage
     */
    public final Stage getStage() {
        return stage;
    }

    /**
     * .
     *
     * @return EStageResultType
     */
    public final EStageResultType getResultType() {
        return resultType;
    }

    /**
     * .
     *
     * @return Location
     */
    public final Location getLocation() {
        return location;
    }

    /**
     * .
     *
     * @return List<StageResultDetail>
     */
    public final List<StageResultDetail> getStageResultDetails() {
        return stageResultDetails;
    }
}
